import axios from 'axios';
import fs from 'fs';

const fileId = '7ryM2G71XRVwkQnkklWrbI';
const token = process.env.FIGMA_ACCESS_TOKEN;

// 需要的图标节点ID（从之前的分析中获取）
const iconNodes = [
  { id: '0:2673', name: 'battery_icon' },      // 电池图标
  { id: '0:2672', name: 'status_bar' },        // 状态栏
  { id: '0:2677', name: 'signal_icon' },       // 信号图标
  { id: '0:2680', name: 'wifi_icon' },         // WiFi图标
];

console.log('🔍 获取状态栏图标...');

for (const icon of iconNodes) {
  try {
    console.log(`\n📱 处理: ${icon.name}`);
    
    const response = await axios.get(`https://api.figma.com/v1/images/${fileId}`, {
      headers: { 'X-Figma-Token': token },
      params: {
        ids: icon.id,
        format: 'png',
        scale: 3,
        svg_include_id: true
      }
    });

    const imageUrl = response.data.images[icon.id];
    
    if (imageUrl) {
      console.log(`下载链接: ${imageUrl}`);
      
      // 下载图片
      const imageResponse = await axios.get(imageUrl, { responseType: 'arraybuffer' });
      const fileName = `${icon.name}.png`;
      fs.writeFileSync(`../entry/src/main/resources/rawfile/${fileName}`, imageResponse.data);
      console.log(`✅ 已下载: ${fileName}`);
    } else {
      console.log(`❌ 无法获取 ${icon.name} 的下载链接`);
    }
    
  } catch (error) {
    console.log(`❌ 处理 ${icon.name} 失败:`, error.response?.status);
  }
}

// 获取插图
console.log('\n🎨 获取登录插图...');
try {
  const illustrationResponse = await axios.get(`https://api.figma.com/v1/images/${fileId}`, {
    headers: { 'X-Figma-Token': token },
    params: {
      ids: '0:2671', // 整个页面
      format: 'png',
      scale: 2
    }
  });

  const illustrationUrl = illustrationResponse.data.images['0:2671'];
  if (illustrationUrl) {
    const imageResponse = await axios.get(illustrationUrl, { responseType: 'arraybuffer' });
    fs.writeFileSync('../entry/src/main/resources/rawfile/phone_login_page.png', imageResponse.data);
    console.log('✅ 已下载: phone_login_page.png');
  }
} catch (error) {
  console.log('❌ 获取插图失败:', error.message);
}
